package com.sihai.core.web.struts2;

import org.apache.commons.lang.StringUtils;

import com.opensymphony.xwork2.ActionSupport;
import com.sihai.core.Limit;
import com.sihai.core.LimitedList;
import com.sihai.core.Paginator;
import com.sihai.core.Sorter;

public class PageSupportAction extends ActionSupport {

	/**
	 * serialVersionUID
	 */
	private static final long serialVersionUID = -7487497527805915476L;

	//========================== 分页支持 ===========================================
	//分页支持
	private Paginator paginator;

	/**
	 * <p/>
	 * 模版页面根据pagination对象完成分页显示
	 *
	 * @return Returns the pagination.
	 */
	public Paginator getPaginator() {
		return this.paginator;
	}

	public void setPaginator(Paginator paginator) {
		this.paginator = paginator;
	}

	/**
	 * 在需要分页的Action.execute中调用此方法完成分页<br>
	 * eg.<br>
	 *
	 * <pre>
	 * 	class SomeAction extends BaseAction{
	 * <p/>
	 * 		private XXXService service;
	 * <p/>
	 * 		public void setXXXService(XXXService service){
	 * 			this.service = service;
	 *         }
	 * <p/>
	 * <p/>
	 * 		public String execute() throws Exception{
	 * 			paging(service.findBySomething(...,getQueryAssistant()));
	 * 			return SUCCESS;
	 *      }
	 *  }
	 *  当Action执行后,Pagination对象生成,此时一些模板引擎(如:freemarker,velocity)可以通过Pagination对象完成分页
	 * </pre>
	 */
	protected void paging(LimitedList<?> limitedList) {
		if (limitedList == null) {
			this.paginator = new Paginator(this.count, 0);
		} else {
			this.paginator = new Paginator(this.count, this.page, limitedList.getTotalcount());
		}
	}

	/**
	 * 根据当前页数和显示记录数得到查询辅助对象,在paging()方法中使用
	 *
	 * @return QueryAssistant 查询辅助对象
	 */
	protected final Limit createLimit() {
		if (page == 0)
			return null;//全部数据
		int startIndex = (page - 1) * count;
		return new Limit(startIndex, count);
	}

	/**
	 * 创建排序对象
	 *
	 * @return
	 */
	protected final Sorter createSorter() {
		Sorter sorter = new Sorter();
		if (StringUtils.isNotBlank(sortProperty)) {
			if (StringUtils.equalsIgnoreCase(sortType, "asc")) {
				sorter.asc(sortProperty);
			} else {
				sorter.desc(sortProperty);
			}
		}
		return sorter;
	}

	/**
	 * 当前页面
	 */
	private int page = 1;

	/**
	 * 显示记录数
	 */
	private int count = 10;

	/**
	 * 排序字段
	 */
	private String sortProperty;

	/**
	 * 排序类型 asc|desc
	 */
	private String sortType;

	/**
	 * <p/>
	 * 每页显示多少记录,默认值为10
	 *
	 * @param count
	 */
	public void setCount(int count) {
		this.count = count;
	}

	public int getCount() {
		return this.count;
	}

	/**
	 * <p/>
	 * 当前是第几页,默认值为1
	 *
	 * @param page
	 */
	public void setPage(int page) {
		this.page = page;
	}

	/**
	 * @return the page
	 */
	public int getPage() {
		return this.page;
	}

	public String getSortProperty() {
		return sortProperty;
	}

	public void setSortProperty(String sortProperty) {
		this.sortProperty = sortProperty;
	}

	public String getSortType() {
		return sortType;
	}

	public void setSortType(String sortType) {
		this.sortType = sortType;
	}

}
